package ca.eloas.judging.rest.resources;

import ca.eloas.judging.messages.CompetitionListMessage;
import ca.eloas.judging.messages.CompetitionMessage;
import ca.eloas.judging.model.CompetitionObject;
import ca.eloas.judging.objectify.PersistenceService;
import ca.eloas.judging.rest.repositories.CompetitionRepository;
import ca.eloas.restsupport.ToMessageOperation;
import ca.eloas.restsupport.utils.BasicResource;
import com.googlecode.objectify.Objectify;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import static ca.eloas.judging.rest.operations.SelfLink.selfLink;

/**
 * @author JP
 */
@Path("/competitions")
public class CompetitionResource extends BasicResource {

    @Inject
    PersistenceService<Objectify> persistence;

    @Inject
    CompetitionRepository competitions;

    @GET
    @Path("/")
    public CompetitionListMessage listCompetitions() {

        return list(competitions.listAll(), CompetitionListMessage.class, CompetitionMessage.class)
            .runOnItems(new ToMessageOperation<CompetitionObject, CompetitionMessage>() {
                public void run(CompetitionObject object, CompetitionMessage competition) {

                    competition.setName(object.getName());
                }
            }).runOnList(
                        selfLink()
                ).now();
    }
}
